import { AuthServer } from "@/inject/core/auth.ts";

const authServer = new AuthServer();
export const hasPermission = (route: any) => {
    if (route.meta && route.meta.roles) {
        return (
            route.meta.roles.filter((cur: any) => {
                const _auth = authServer.checkAuth({
                    [cur]: []
                });
                return _auth;
            }).length > 0
        );
    } else {
        return true;
    }
};
export const getPermissionList = (routes: any[]) => {
    const res: any[] = [];
    routes.forEach(route => {
        const tmp = { ...route };
        if (hasPermission(tmp)) {
            if (tmp.children) {
                // TODO add get backMenuList format name to title in meta
                tmp.children = getPermissionList(tmp.children);
            }
            res.push(tmp);
        }
    });

    return res;
};
